﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XQ.Framework.html;

namespace XQ.Framework.core
{
    public class Pager
    {
        //这里都是只读参数
        public int Page { get; private set; }
        public int PageSize { get; private set; }
        public int ItemCount { get; private set; }
        //上面是设置的参数
        //下面是计算得出的结果
        public int PageCount { get; private set; }
        public string MySqlLimitMin { get; private set; }
        public string MySqlLimitLen { get; private set; }
        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="current_page"></param>
        /// <param name="page_size"></param>
        /// <param name="item_count"></param>
        public Pager(int current_page, int page_size, int item_count)
        {
            SetData(current_page, page_size, item_count);
        }
        /// <summary>
        /// 过滤输入
        /// </summary>
        private void FilterData()
        {
            if (Page < 1) { Page = 1; }
            if (PageSize < 1) { PageSize = 1; }
            if (ItemCount < 0) { ItemCount = 0; }
        }
        /// <summary>
        /// 设置计算参数
        /// </summary>
        /// <param name="current_page"></param>
        /// <param name="page_size"></param>
        /// <param name="item_count"></param>
        public void SetData(int current_page, int page_size, int item_count)
        {
            this.Page = current_page;
            this.PageSize = page_size;
            this.ItemCount = item_count;
            FilterData();
            //计算结果
            this.PageCount = Pager.GetPageCount(this.ItemCount, this.PageSize);
            string[] limit = Pager.GetMySqlLimit(this.Page, this.PageSize);
            this.MySqlLimitMin = limit[0];
            this.MySqlLimitLen = limit[1];
        }
        public string GetPagerHtml(string url, bool showCount = true)
        {
            return PagerHelper.GetSimplePagerHtml(Page, PageCount, url, showCount);
        }
        /// <summary>
        /// 计算总页数
        /// </summary>
        /// <param name="item_count"></param>
        /// <param name="page_size"></param>
        /// <returns></returns>
        public static int GetPageCount(int item_count, int page_size)
        {
            if (item_count < 0) { item_count = 0; }
            if (page_size < 1) { page_size = 1; }
            return item_count % page_size == 0 ? (item_count / page_size) : (item_count / page_size + 1);
        }
        /// <summary>
        /// 计算mysql 的limit参数
        /// </summary>
        /// <param name="current_page"></param>
        /// <param name="page_size"></param>
        /// <returns></returns>
        public static string[] GetMySqlLimit(int current_page, int page_size)
        {
            if (current_page < 1) { current_page = 1; }
            if (page_size < 1) { page_size = 1; }
            return new string[] { ((current_page - 1) * page_size).ToString(), page_size.ToString() };
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="current_page"></param>
        /// <param name="page_size"></param>
        /// <returns></returns>
        public static string[] GetLimitOffset(int current_page, int page_size)
        {
            if (current_page < 1) { current_page = 1; }
            if (page_size < 1) { page_size = 1; }
            return new string[] { ((current_page - 1) * page_size).ToString(), page_size.ToString() };
        }
    }
}
